var express = require('express');
var router = express.Router();
var articleModel = require('../tool/model/article.js');
router.get('/', (req, res, next)=> {
  res.render('manarticle',{
    roots: req.cookies.root,
  })    // res.end()
});
router.get('/article', (req, res, next)=> {
  articleModel.aggregate([
    {
      $lookup:
        {
          from: "user",
          localField: "articleMark",
          foreignField: "userID",
          as: "data"
        }
   },{
    $match:{"status": {"$lt": 3},'articleMark':{'$eq':req.cookies.id}}
   }],function(err,docs){
    if(err){
      console.log(err);
      return;
  }
    console.log(docs)
   
    res.send({
      data:docs
      })
  })

});
router.post('/articleupdate',function(req, res, next) {
  let data = req.body; 
      title=data.title,
      desc=data.desc,
      kind=data.kind,
      status=data.status,
      id=data.id,
      contentUrl=data.contentUrl,
      content= data.content
  console.log(data)
   articleModel.update({
     _id: data.id}, 
     {
       $set:{
       'title':title,
        'desc':desc,
        'kind':kind,
        'status':status,
        'contentUrl':contentUrl,
        'content':content}
      },(err,docs)=>{
    if (err) {
      res.send({
          status: '1',
          type:'error',
          message: error.message,
          result: 'err'
      })
  } else {
      res.send({
          status: '0',
          type:'success',
          message: '更改成功！',
          result: 'suc'
      })
    }
   })
});
router.post('/articledel',function(req, res, next) {
  let data = req.body; 
      status=data.status,
      id=data.id,
  console.log(data)
   articleModel.update({
     _id: data.id}, 
     {'status':status},(err,docs)=>{
    if (err) {
      res.send({
          status: '1',
          type:'error',
          message: error.message,
          result: 'err'
      })
  } else {
      res.send({
          status: '0',
          type:'success',
          message: '更改成功！',
          result: 'suc'
      })
    }
   })
});
module.exports = router;